<!--
 * @Description: 混凝土检测
 * @Author: wangwangwang
 * @Date: 2020-11-18 15:34:16
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-05-07 10:37:45
-->
<template>
  <div class="flows-content">
    <el-container>
      <el-main style="margin-bottom: 20px; padding: 0;">
        <el-container direction="vertical">
          <FlowHeader :isReadOnly="readOnly" @saveSubmit="handleSubmit(1)" @sendSubmit="handleSubmit(2)" @handleBackEnd="handleSubmit(3)" @handleCC="handleSubmit(4)" @handleAssign="handleSubmit(5)" @handleBackUp="handleSubmit(6)" @handleAssist="handleSubmit(7)" @handleNode="handleSubmit(8)" @handleNodeImage="handleSubmit(9)" @handleCollection="handleSubmit(10)" />
          <el-main class="info">
            <div>
              <el-input v-model="title" class="flow-title" :disabled="$route.query.type*1!==1" placeholder="请输入流程标题"></el-input>
            </div>
            <div class="info-top">
              <div class="left">
                <span>单据号码：{{billCode}}</span>
                <span class="text">制单日期：{{billDate}}</span>
              </div>
              <div class="right">
                <span>发起人：{{applyPerson}}</span>
                <span class="text">状态：{{taskTypeText[taskStatus*1]}}</span>
              </div>
            </div>
            <div style="margin-top:20px;">
              <div class="flows-details">
                <el-form :model="form" label-width="150px" :disabled='readOnly'>
                  <el-row>
                    <el-col :span="12">
                      <el-form-item label="系统检测结果">
                        <span>NG</span>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="处理方式">
                        <el-select v-model="form.handling" placeholder="请选择处理方式">
                          <el-option v-for="item in handleTypeList" :key="item.itemName" :label="item.itemName" :value="item.itemName"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="12">
                      <el-form-item label="站点">
                        <span>{{formData.station}}</span>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="工程名称">
                        <span>{{formData.engineeringName}}</span>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="12">
                      <el-form-item label="产品名称">
                        <span>{{formData.product}}</span>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="车牌号">
                        <span>{{formData.carNo}}</span>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
              <div>
                <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableSource" tooltip-effect="dark" height="100%">
                  <template v-for="(item, index) in tableNav">
                    <el-table-column :key="index" :label="item.NAME || item.name" align="center" :width="index == 0 ? '120px' : 'auto'">
                      <template slot-scope="scope">
                        <span v-if="index == 0">{{ scope.row[index].name || scope.row[index].testItem }}</span>
                        <div v-if="index !== 0 && scope.row[index]">
                          <el-select v-if="(scope.row[index].dataType||scope.row[index].type) == 1" v-model="scope.row[index].testValue" placeholder="请选择" style="width:100%" disabled>
                            <el-option v-for="(item, index) in scope.row[index].list" :key="index" :label="item.name" :value="item.name"></el-option>
                          </el-select>
                          <el-input v-if="(scope.row[index].dataType||scope.row[index].type) == 2" v-model="scope.row[index].testValue" disabled style="width:100%" placeholder="请输入" clearable></el-input>
                          <span v-if="(scope.row[index].dataType||scope.row[index].type) == 3">
                            <span>{{ scope.row[index].symbol }}</span>
                            <span>
                              <el-input v-model="scope.row[index].testValue" disabled type="Number" style="width: 90px;margin-left: 5px;" placeholder="请输入" clearable></el-input>
                            </span>
                          </span>
                        </div>
                      </template>
                    </el-table-column>
                  </template>
                </el-table>
              </div>
            </div>
            <div class="flows-details">
              <FlowContent ref="flowcontent" :title='title' :showSendVisible="showSendVisible" @handleCancel="handleCancel" @sendCode="sendCode" @handleFinish="handleFinish" @handleBusiness="handleBusiness" @submitData="submitDataType" @resetFlowState="resetFlowState" />
            </div>
          </el-main>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import FlowContent from '@/components/WorkFlow/flowContent';
import FlowHeader from '@/components/WorkFlow/flowHeader';
import { saveCheckOutResult, makeupdStatus2 } from '@/api/smart/checkout';
import { getDicsType } from "@/api/common.js";
import addTab from "@/common/addTab.js";
import { getUserInfo } from "@/utils/auth.js";
export default {
  components: {
    FlowContent,
    FlowHeader,
  },
  data() {
    return {
      type: 0,
      title: '混凝土检测',
      billCode: '',
      billDate: '',
      applyPerson: '',
      taskTypeText: ['发起', '已保存', '审批中', '已完成', '撤销', '不通过'],
      taskStatus: 0,
      showSendVisible: false,
      readOnly: false,
      formData: {},
      form: {
        handling: '',
        type: '1',
      },
      tableSource: [],
      tableNav: [],
      handleTypeList: [],
    }
  },
  methods: {
    submitDataType() {
      makeupdStatus2({
        id: this.formData.id,
        detectionStatus: 3,
      });
    },
    resetFlowState (type) {
      makeupdStatus2({
        id: this.formData.id,
        dataStatus: type===1?4:5,
      });
    },
    handleBusiness() {
      const { query } = this.$route;
      const data = JSON.parse(query.dataContent);
      const newData = this.handleTypeList.find(item => item.itemName === this.form.handling);
      const params = {
        process_success: {
          url: '/icps/system/sign/intelligent/concrete/updateExceptionHandling',
          data: {
            id: data.id,
            exceptionHandling: newData ?.itemName,
            exceptionHandlingId: newData ?.id,
          },
          httpMethod: 'post',
        },
        process_failure: ''
      }
      this.showSendVisible = false;
      this.$refs.flowcontent.handleToFlow(params);
    },
    handleFinish(type, status) {
      const { query } = this.$route;
      const data = JSON.parse(query.dataContent);
      const copyData = JSON.parse(JSON.stringify(this.tableNav));
      copyData.shift();
      if (type === 2 && status * 1 === 2) {
        const params = {
          id: data.id,
          type: 2,
          typeId: data.dataTypeId,
          claim: copyData,
          remark: data.remark,
          outcome: 'NG',
          list: this.tableSource,
        }
        saveCheckOutResult(params);
      }
      addTab.$emit("remove", this.$route.name);
    },
    handleSubmit(type) {
      const self = this;
      const { query } = this.$route;
      if (query.type * 1 === 2 && type === 2 && query.taskType * 1 !== 2) { // 发起流程
        this.showSendVisible = true;
        return
      }
      if (query.type * 1 === 10) { // 强制编辑
        const data = JSON.parse(query.dataContent);
        const copyData = JSON.parse(JSON.stringify(this.tableNav));
        copyData.shift();
        this.$confirm('确定强制修改此流程，修改后业务数据将生效?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'bottom_all_two',
          confirmButtonClass: 'bottom_all',
          type: 'warning'
        }).then(() => {
          const params = {
            id: data.id,
            type: 2,
            typeId: data.dataTypeId,
            claim: copyData,
            remark: data.remark,
            outcome: 'NG',
            list: self.tableSource,
          }
          saveCheckOutResult(params).then(res => {
            if (res.code === 1001) {
              const params = {
                dataSource: 'checkflows2',
                dataContent: JSON.stringify({ ...self.formData, tableTitle: this.formData.tableTitle, tableDataSource: this.formData.tableDataSource, form: JSON.stringify(self.form) }), // 业务数据
              }
              self.$refs.flowcontent.saveFlows(type, params, query.type);
            }
          });
        })
        return
      }
      const params = {
        dataSource: 'checkflows2',
        dataContent: JSON.stringify({ ...this.formData, tableTitle: query.tableTitle, tableDataSource: query.tableDataSource, form: JSON.stringify(this.form) }), // 业务数据
      }
      this.$refs.flowcontent.saveFlows(type, params);
    },
    handleFlowChat() {
      this.$refs.flowcontent.showFlowNode()
    },
    handleCancel() {
      this.showSendVisible = false;
    },
    /**
     * @Description: 回显业务数据
     * @Param: 
     * @Author: wangwangwang
     */
    initFlowsData(data) {
      const { query } = this.$route;
      this.formData = data;
      this.tableNav = JSON.parse(query.tableTitle);
      this.tableSource = JSON.parse(query.tableDataSource);
    },
    initFlowsData2() {
      const { query } = this.$route;
      this.formData = JSON.parse(query.dataContent);
      this.tableNav = JSON.parse(this.formData.tableTitle);
      this.tableSource = JSON.parse(this.formData.tableDataSource);
      this.form = JSON.parse(this.formData.form);
      this.title = query.taskName;
      this.billCode = query.receiptNumber;
      this.billDate = query.createTime;
      this.applyPerson = query.createUserName;
      this.taskStatus = query.taskStatus;
    },
    sendCode(code) {
      this.billCode = code;
    },
  },
  mounted() {
    const { query } = this.$route;
    if (query.type * 1 === 1) { // 发起流程
      this.initFlowsData(query);
      /**
       * @Description: 判断为哪种流程
       * @Param: 
       * @Author: wangwangwang
       */
      let code = 'OtherCheck2';
      if (query.flowsType * 1 === 1) {
        code = 'EnterCheck'
      }
      this.$refs.flowcontent.initBaseData(code);
      this.billDate = new Date().Format('yyyy-MM-dd hh:mm:ss');
      this.applyPerson = getUserInfo().userRealName;
      return
    }
    this.initFlowsData2();
    if (query.isReadOnly*1===1) {
      this.readOnly = true;
    }
  },
  created() {
    getDicsType({ codes: "handleType" }).then((res) => {
      if (res.code === 1001) {
        this.handleTypeList = res.data ?.handleType ?.items;
      }
    });
  },
}
</script>
<style lang="scss" scoped>
.flows-details {
  margin-top: 20px;
}
.info {
  padding: 0 20px;
  background: #fff;
  .info-top {
    font-size: $font_size;
    color: #000000;
    @include discen_al;
    justify-content: space-between;
    height: 50px;
    border-bottom: 1px #e1e5ed solid;
    .left {
      @include discen_al;
    }
    .text {
      margin-left: 60px;
    }
  }
}
.nav_title {
  @include discen_al;
  align-items: flex-end;
  > div {
    width: 4px;
    height: 24px;
    background: $bg_color;
    margin-right: 10px;
  }
  i {
    margin-left: 5px;
  }
  color: $bg_color;
  font-size: 20px;
  font-weight: bold;
}
</style>
